<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="算法名称" prop="algorithmName">
        <el-input
          v-model="queryParams.algorithmName"
          placeholder="请输入算法名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="算法作者" prop="algorithmAuthor">
        <el-input
          v-model="queryParams.algorithmAuthor"
          placeholder="请输入算法作者"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="算法类型" prop="algorithmType">
        <el-select v-model="queryParams.algorithmType" placeholder="请选择算法类型" clearable>
          <el-option
            v-for="dict in dict.type.algorithm_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增
        </el-button>
      </el-col>
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="success"-->
      <!--          plain-->
      <!--          icon="el-icon-edit"-->
      <!--          size="mini"-->
      <!--          :disabled="single"-->
      <!--          @click="handleUpdate"-->
      <!--        >修改</el-button>-->
      <!--      </el-col>-->
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="danger"-->
      <!--          plain-->
      <!--          icon="el-icon-delete"-->
      <!--          size="mini"-->
      <!--          :disabled="multiple"-->
      <!--          @click="handleDelete"-->
      <!--        >删除</el-button>-->
      <!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!--    <el-table v-loading="loading" :data="principleList" @selection-change="handleSelectionChange">-->
    <!--      <el-table-column type="selection" width="55" align="center" />-->
    <!--      <el-table-column label="序号" type="index" width="50"/>-->
    <!--      <el-table-column label="算法名称" align="center" prop="algorithmName" />-->
    <!--      <el-table-column label="算法作者" align="center" prop="algorithmAuthor" />-->
    <!--      <el-table-column label="模块类型" align="center" prop="moduleType">-->
    <!--        <template slot-scope="scope">-->
    <!--          <dict-tag :options="dict.type.module_type" :value="scope.row.moduleType"/>-->
    <!--        </template>-->
    <!--      </el-table-column>-->
    <!--      <el-table-column label="算法类型" align="center" prop="algorithmType">-->
    <!--        <template slot-scope="scope">-->
    <!--          <dict-tag :options="dict.type.algorithm_type" :value="scope.row.algorithmType"/>-->
    <!--        </template>-->
    <!--      </el-table-column>-->
    <!--      <el-table-column label="算法简介" align="center" prop="algorithmIntro" />-->
    <!--      <el-table-column label="算法步骤" align="center" prop="algorithmSteps" />-->
    <!--      <el-table-column label="算法功能" align="center" prop="algorithmFunction" />-->
    <!--      <el-table-column label="算法流程" align="center" prop="fileName" />-->
    <!--      <el-table-column label="相对路径" align="center" prop="relativePath" />-->
    <!--      <el-table-column label="绝对路径" align="center" prop="absolutePath" />-->
    <!--      <el-table-column label="文件链接" align="center" prop="fileUrl" />-->
    <!--      <el-table-column label="文件大小" align="center" prop="fileSize" />-->
    <!--      <el-table-column label="状态" align="center" prop="status">-->
    <!--        <template slot-scope="scope">-->
    <!--          <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.status"/>-->
    <!--        </template>-->
    <!--      </el-table-column>-->
    <!--      <el-table-column label="备注" align="center" prop="remark" />-->
    <!--      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">-->
    <!--        <template slot-scope="scope">-->
    <!--          <el-button-->
    <!--            size="mini"-->
    <!--            type="text"-->
    <!--            icon="el-icon-edit"-->
    <!--            @click="handleUpdate(scope.row)"-->
    <!--          >修改</el-button>-->
    <!--          <el-button-->
    <!--            size="mini"-->
    <!--            type="text"-->
    <!--            icon="el-icon-delete"-->
    <!--            @click="handleDelete(scope.row)"-->
    <!--          >删除</el-button>-->
    <!--        </template>-->
    <!--      </el-table-column>-->
    <!--    </el-table>-->

    <el-row v-loading="loading" style="background-color: #FFFFFF" :gutter="24">
      <el-col v-for="(item, index) in principleList" :span="6" :key="index">
        <el-card :body-style="{ padding: '8px',background: '#f4f4f5' }" shadow="hover">
          <div class="item-img">
            <div @click="handleDetail(item)">
              <el-image :src="item.relativePath" fit="fill" style="width: 100%; height: 180px">
                <div slot="error" class="image-slot">
                  <i class="el-icon-picture-outline"></i>
                </div>
              </el-image>
            </div>
          </div>
          <el-row>
            <el-col :span="24">
              <div class="item-text" :title="item.algorithmName">
                算法名称：<span class="algorithmName">{{ item.algorithmName }}</span>
              </div>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <div class="item-text" :title="item.algorithmAuthor" style="text-indent: 2em;">
                作者：<span class="algorithmAuthor">{{ item.algorithmAuthor }}</span>
              </div>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <div class="item-text" :title="item.algorithmFunction">
                算法功能：<span class="algorithmFunction">{{ item.algorithmFunction }}</span>
              </div>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(item)"
              >修改
              </el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(item)"
              >删除
              </el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-more"
                @click="handleDetail(item)"
              >详情
              </el-button>
            </el-col>
          </el-row>
        </el-card>
      </el-col>
    </el-row>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改算法原理管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="840px" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="88px" class="form-overflow-hide">
        <el-row>
          <el-col :span="12">
            <el-form-item label="算法名称" prop="algorithmName">
              <el-input v-model="form.algorithmName" placeholder="请输入算法名称"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="算法作者" prop="algorithmAuthor">
              <el-input v-model="form.algorithmAuthor" placeholder="请输入算法作者"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="算法功能" prop="algorithmFunction">
              <el-input v-model="form.algorithmFunction" placeholder="请输入算法功能"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="算法类型" prop="algorithmType">
              <el-select v-model="form.algorithmType" placeholder="请选择算法类型" style="width: 100%">
                <el-option
                  v-for="dict in dict.type.algorithm_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="算法简介" prop="algorithmIntro">
              <el-input v-model="form.algorithmIntro" type="textarea" placeholder="请输入内容" maxlength="255"
                        show-word-limit
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="算法步骤">
              <editor v-model="form.algorithmSteps" :min-height="130"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="算法流程文件名" prop="fileName" label-width="120px">
              <el-input v-model="form.fileName" placeholder="请输入上传算法流程文件" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="算法流程文件" label-width="120px">
              <file-upload :limit="1" :file-type="['gif', 'jpg', 'jpeg', 'png']" :is-show-tip="false"
                           @multipleInput="getFileUpload" :open-time="new Date().getTime()"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" maxlength="255" show-word-limit/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 算法原理管理详情对话框 -->
    <el-dialog title="详情" :visible.sync="detailOpen" width="840px" append-to-body>
      <el-form ref="form" :model="form" label-width="88px" class="form-overflow-hide">
        <el-row>
          <el-col :span="12">
            <el-form-item label="算法名称" prop="algorithmName">
              {{ form.algorithmName }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="算法作者" prop="algorithmAuthor">
              {{ form.algorithmAuthor }}
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="算法功能" prop="algorithmFunction">
              {{ form.algorithmFunction }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="算法类型" prop="algorithmType">
              <dict-tag :options="dict.type.algorithm_type" :value="form.algorithmType"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="算法简介" prop="algorithmIntro">
              {{ form.algorithmIntro }}
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="算法步骤">
              <div v-html="form.algorithmSteps" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="22">
            <el-form-item label="算法流程" prop="fileName" label-width="120px">
              <ImagePreview :src="form.relativePath" :width="'100%'" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="备注" prop="remark">
          {{ form.remark }}
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="detailOpen = false">关 闭</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { listPrinciple, getPrinciple, delPrinciple, addPrinciple, updatePrinciple } from '@/api/knowledge/principle'

export default {
  name: 'Principle',
  dicts: ['module_type', 'algorithm_type', 'sys_normal_disable'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 算法原理管理表格数据
      principleList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      detailOpen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        algorithmName: null,
        algorithmAuthor: null,
        moduleType: null,
        algorithmType: null,
        algorithmIntro: null,
        algorithmSteps: null,
        algorithmFunction: null,
        fileName: null,
        relativePath: null,
        absolutePath: null,
        fileUrl: null,
        fileSize: null,
        status: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        algorithmName: [
          { required: true, message: '算法名称不能为空', trigger: 'blur' }
        ],
        algorithmAuthor: [
          { required: true, message: '算法作者不能为空', trigger: 'blur' }
        ],
        moduleType: [
          { required: true, message: '模块类型不能为空', trigger: 'change' }
        ],
        algorithmType: [
          { required: true, message: '算法类型不能为空', trigger: 'change' }
        ],
        fileName: [
          { required: true, message: '算法流程不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询算法原理管理列表 */
    getList() {
      this.loading = true
      this.queryParams.moduleType = sessionStorage.getItem('moduleType')
      listPrinciple(this.queryParams).then(response => {
        this.principleList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        principleId: null,
        algorithmName: null,
        algorithmAuthor: null,
        moduleType: null,
        algorithmType: null,
        algorithmIntro: null,
        algorithmSteps: null,
        algorithmFunction: null,
        fileName: null,
        relativePath: null,
        absolutePath: null,
        fileUrl: null,
        fileSize: null,
        delFlag: null,
        status: '0',
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      }
      this.form.moduleType = sessionStorage.getItem('moduleType')
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.principleId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加算法原理管理'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const principleId = row.principleId || this.ids
      getPrinciple(principleId).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改算法原理管理'
      })
    },
    getFileUpload(res) {
      if (res.length > 0) {
        let file = res[0]
        this.form.fileName = file.originalFilename
        this.form.relativePath = file.fileName
        this.form.absolutePath = file.absolutePath
        this.form.fileUrl = file.url
        this.form.fileSize = file.fileSize
      }
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.principleId != null) {
            updatePrinciple(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addPrinciple(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const principleIds = row.principleId || this.ids
      this.$modal.confirm('是否确认删除算法原理管理编号为"' + principleIds + '"的数据项？').then(function() {
        return delPrinciple(principleIds)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    },
    handleDetail(row) {
      this.reset()
      const principleId = row.principleId || this.ids
      getPrinciple(principleId).then(response => {
        this.form = response.data
        this.detailOpen = true
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('knowledge/principle/export', {
        ...this.queryParams
      }, `principle_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
<style scoped lang="scss">
.item-img {
  width: 60%;
  height: 200px;
  margin: 0 auto;
  padding: 8px 0 0 0;
  text-align: center;
}

.form-overflow-hide {
  height: 70vh;
  padding: 8px;
  overflow-y: auto;
}

.item-text {
  color: #606266;
  font-size: 14px;
  white-space:nowrap;
  overflow:hidden;
  text-overflow:ellipsis;
  .algorithmName {
    color: #303133;
    font-size: 16px;
  }
  .algorithmAuthor {
    color: #303133;
    font-size: 18px;
  }
  .algorithmFunction {
    font-size: 16px;
  }
}
</style>
